<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="优惠卷类型 1 现金券 2 折扣" prop="couponType">
      <el-input v-model="dataForm.couponType" placeholder="优惠卷类型 1 现金券 2 折扣"></el-input>
    </el-form-item>
    <el-form-item label="优惠卷名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="优惠卷名字"></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="amount">
      <el-input v-model="dataForm.amount" placeholder="金额"></el-input>
    </el-form-item>
    <el-form-item label="折扣：取值[1 到 10]" prop="discount">
      <el-input v-model="dataForm.discount" placeholder="折扣：取值[1 到 10]"></el-input>
    </el-form-item>
    <el-form-item label="使用门槛 0->没门槛" prop="conditionAmount">
      <el-input v-model="dataForm.conditionAmount" placeholder="使用门槛 0->没门槛"></el-input>
    </el-form-item>
    <el-form-item label="发行数量，0->无限制" prop="publishCount">
      <el-input v-model="dataForm.publishCount" placeholder="发行数量，0->无限制"></el-input>
    </el-form-item>
    <el-form-item label="每人限领张数，0-不限制 1-限领1张 2-限领2张" prop="perLimit">
      <el-input v-model="dataForm.perLimit" placeholder="每人限领张数，0-不限制 1-限领1张 2-限领2张"></el-input>
    </el-form-item>
    <el-form-item label="已使用数量" prop="useCount">
      <el-input v-model="dataForm.useCount" placeholder="已使用数量"></el-input>
    </el-form-item>
    <el-form-item label="领取数量" prop="receiveCount">
      <el-input v-model="dataForm.receiveCount" placeholder="领取数量"></el-input>
    </el-form-item>
    <el-form-item label="过期时间" prop="expireTime">
      <el-input v-model="dataForm.expireTime" placeholder="过期时间"></el-input>
    </el-form-item>
    <el-form-item label="优惠券描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="优惠券描述"></el-input>
    </el-form-item>
    <el-form-item label="状态[0-未发布，1-已发布， -1-已过期]" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态[0-未发布，1-已发布， -1-已过期]"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="删除标记（0:不可用 1:可用）" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="删除标记（0:不可用 1:可用）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          couponType: '',
          name: '',
          amount: '',
          discount: '',
          conditionAmount: '',
          publishCount: '',
          perLimit: '',
          useCount: '',
          receiveCount: '',
          expireTime: '',
          description: '',
          status: '',
          createTime: '',
          updateTime: '',
          isDeleted: ''
        },
        dataRule: {
          couponType: [
            { required: true, message: '优惠卷类型 1 现金券 2 折扣不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '优惠卷名字不能为空', trigger: 'blur' }
          ],
          amount: [
            { required: true, message: '金额不能为空', trigger: 'blur' }
          ],
          discount: [
            { required: true, message: '折扣：取值[1 到 10]不能为空', trigger: 'blur' }
          ],
          conditionAmount: [
            { required: true, message: '使用门槛 0->没门槛不能为空', trigger: 'blur' }
          ],
          publishCount: [
            { required: true, message: '发行数量，0->无限制不能为空', trigger: 'blur' }
          ],
          perLimit: [
            { required: true, message: '每人限领张数，0-不限制 1-限领1张 2-限领2张不能为空', trigger: 'blur' }
          ],
          useCount: [
            { required: true, message: '已使用数量不能为空', trigger: 'blur' }
          ],
          receiveCount: [
            { required: true, message: '领取数量不能为空', trigger: 'blur' }
          ],
          expireTime: [
            { required: true, message: '过期时间不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '优惠券描述不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态[0-未发布，1-已发布， -1-已过期]不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '删除标记（0:不可用 1:可用）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/couponinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.dataForm.couponType = data.couponInfo.couponType
                this.dataForm.name = data.couponInfo.name
                this.dataForm.amount = data.couponInfo.amount
                this.dataForm.discount = data.couponInfo.discount
                this.dataForm.conditionAmount = data.couponInfo.conditionAmount
                this.dataForm.publishCount = data.couponInfo.publishCount
                this.dataForm.perLimit = data.couponInfo.perLimit
                this.dataForm.useCount = data.couponInfo.useCount
                this.dataForm.receiveCount = data.couponInfo.receiveCount
                this.dataForm.expireTime = data.couponInfo.expireTime
                this.dataForm.description = data.couponInfo.description
                this.dataForm.status = data.couponInfo.status
                this.dataForm.createTime = data.couponInfo.createTime
                this.dataForm.updateTime = data.couponInfo.updateTime
                this.dataForm.isDeleted = data.couponInfo.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/couponinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'couponType': this.dataForm.couponType,
                'name': this.dataForm.name,
                'amount': this.dataForm.amount,
                'discount': this.dataForm.discount,
                'conditionAmount': this.dataForm.conditionAmount,
                'publishCount': this.dataForm.publishCount,
                'perLimit': this.dataForm.perLimit,
                'useCount': this.dataForm.useCount,
                'receiveCount': this.dataForm.receiveCount,
                'expireTime': this.dataForm.expireTime,
                'description': this.dataForm.description,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
